package com.hexacta.radio.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.hexacta.radio.controller.Controller;

public class RadioView {

	private static final String EMPTY_STRING = "";
	private JFrame frame;
	private JTextField display;
	private JButton dialDown;
	private JButton dialUp;
	private JButton seekDown;
	private Controller controller;
	private JCheckBox checkBox;
	private JInternalFrame editDialFrame;
	private JTextField nameTextField;
	private JTextField dialTextField;

	public RadioView(Controller controller) {
		this.controller = controller;
		this.initializeControls();
	}

	private void initializeControls() {
		this.frame = new JFrame();
		this.frame.setBounds(200, 200, 500, 500);
		this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		this.checkBox = new JCheckBox(EMPTY_STRING);
		checkBox.setBounds(6, 7, 35, 23);
		frame.getContentPane().add(checkBox);
		checkBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent changeEvent) {
				controller.turnRadioOnOff(checkBox.isSelected());
			}
		});
		
		display = new JTextField();
		display.setBounds(47, 10, 427, 20);
		display.setEnabled(false);
		frame.getContentPane().add(display);
		display.setColumns(10);
		display.setHorizontalAlignment(JTextField.RIGHT);
		
		dialDown = new JButton("<<");
		dialDown.setBounds(47, 37, 50, 23);
		frame.getContentPane().add(dialDown);
		dialDown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				controller.dialDown();
			}
		});
		
		dialUp = new JButton(">>");
		dialUp.setBounds(100, 37, 50, 23);
		frame.getContentPane().add(dialUp);
		dialUp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				controller.dialUp();
			}
		});
		
		seekDown = new JButton("<<<");
		seekDown.setBounds(350, 41, 57, 23);
		frame.getContentPane().add(seekDown);
		seekDown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				controller.seekDown();
			}
		});
		
		JButton seekUp = new JButton(">>>");
		seekUp.setBounds(417, 41, 57, 23);
		frame.getContentPane().add(seekUp);
		seekUp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				controller.seekUp();
			}
		});
		
		JList favouritesList = new JList();
		favouritesList.setBounds(10, 214, 464, -120);
		frame.getContentPane().add(favouritesList);
		
		JButton editDial = new JButton("Editar Emisora");
		editDial.setBounds(350, 71, 124, 23);
		frame.getContentPane().add(editDial);
		editDial.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.editSelectedDial();
			}
		});
		
		JButton btnAgregarAFavoritos = new JButton("Agregar a Favoritos");
		btnAgregarAFavoritos.setBounds(47, 71, 141, 23);
		frame.getContentPane().add(btnAgregarAFavoritos);
		btnAgregarAFavoritos.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.addDialToFavourites();
			}
		});
		
		/**
		 * Edit Dial View
		 */
		this.editDialFrame = new JInternalFrame("Editar Emisora", true, true, true);
		this.editDialFrame.setBounds(200, 200, 250, 150);
		this.editDialFrame.setLayout(new GridBagLayout());
		this.editDialFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		
		GridBagConstraints c = new GridBagConstraints();
		JLabel dialLabel = new JLabel("Emisora");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		this.editDialFrame.add(dialLabel, c);
		
		dialTextField = new JTextField();
		dialTextField.setEnabled(false);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 2; // two columns
		this.editDialFrame.add(dialTextField, c);
		
		JLabel nameLabel = new JLabel("Descripcion");
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		this.editDialFrame.add(nameLabel, c);
		
		nameTextField = new JTextField();
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 2; // two columns
		this.editDialFrame.add(nameTextField, c);
		
		JButton acceptButton = new JButton("Aceptar");
		acceptButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.acceptEditDial(dialTextField.getText(), nameTextField.getText());
				
			}
		});
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 1;
		this.editDialFrame.add(acceptButton, c);
		
		JButton cancelButton = new JButton("Cancelar");
		cancelButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.cancelEditDial();
			}
		});
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 2;
		c.gridwidth = 1;
		this.editDialFrame.add(cancelButton, c);
		
		this.frame.add(editDialFrame);
	}
	
	public void showEditDial(String dial) {
		this.editDialFrame.setVisible(true);
		this.dialTextField.setText(dial);
		this.nameTextField.setText(EMPTY_STRING);
	}
	
	public void closeEditDial() {
		this.editDialFrame.setVisible(false);
	}

	public void setVisible(boolean visibility) {
		this.frame.setVisible(visibility);
	}

	public void updateDisplay(String dial) {
		this.display.setText(dial);
		this.display.repaint();
	}
}
